/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package roomarrange;

import com.sun.data.provider.impl.CachedRowSetDataProvider;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.webui.jsf.component.Calendar;
import com.sun.webui.jsf.component.DropDown;
import com.sun.webui.jsf.component.Listbox;
import com.sun.webui.jsf.component.TextField;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;
import javax.faces.context.FacesContext;
import javax.faces.convert.IntegerConverter;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import javax.sql.rowset.CachedRowSet;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version approveW2.java
 * @version Created on 2010-4-26, 16:18:55
 * @author ben
 */
public class approveW2 extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
        t_hardwareDataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.t_hardwareRowSet}"));
        v_attendeeDataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.v_attendeeRowSet}"));
    }
    private Calendar calendar1 = new Calendar();

    public Calendar getCalendar1() {
        return calendar1;
    }

    public void setCalendar1(Calendar c) {
        this.calendar1 = c;
    }
    private DropDown dropDown1 = new DropDown();

    public DropDown getDropDown1() {
        return dropDown1;
    }

    public void setDropDown1(DropDown dd) {
        this.dropDown1 = dd;
    }
    private Calendar calendar2 = new Calendar();

    public Calendar getCalendar2() {
        return calendar2;
    }

    public void setCalendar2(Calendar c) {
        this.calendar2 = c;
    }
    private DropDown dropDown2 = new DropDown();

    public DropDown getDropDown2() {
        return dropDown2;
    }

    public void setDropDown2(DropDown dd) {
        this.dropDown2 = dd;
    }

    // </editor-fold>
    /**
     * <p>Construct a new Page bean instance.</p>
     */
    int bgs = 0; //办公室
    int jsk = 0; //技术科
    int kmr = 0; //开门人
    int fjd = 0; //法警队长
    int jcz = 0; //主管检察长
    meetings mt = new meetings();
    //生成临时数据
    ApplicationContext ctx = new ClassPathXmlApplicationContext("SpringConfig.xml");
    DataSource ds = (DataSource) ctx.getBean("dataSourceDBDirect");
    //DataSource ds = (DataSource) ctx.getBean("dataSourcePool");
    DataJdbcDao taskDao = new DataJdbcDao();
    private TextField textField3 = new TextField();

    public TextField getTextField3() {
        return textField3;
    }

    public void setTextField3(TextField tf) {
        this.textField3 = tf;
    }
    private CachedRowSetDataProvider t_hardwareDataProvider = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getT_hardwareDataProvider() {
        return t_hardwareDataProvider;
    }

    public void setT_hardwareDataProvider(CachedRowSetDataProvider crsdp) {
        this.t_hardwareDataProvider = crsdp;
    }
    private CachedRowSetDataProvider v_attendeeDataProvider = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getV_attendeeDataProvider() {
        return v_attendeeDataProvider;
    }

    public void setV_attendeeDataProvider(CachedRowSetDataProvider crsdp) {
        this.v_attendeeDataProvider = crsdp;
    }
    private IntegerConverter listbox1Converter = new IntegerConverter();

    public IntegerConverter getListbox1Converter() {
        return listbox1Converter;
    }

    public void setListbox1Converter(IntegerConverter ic) {
        this.listbox1Converter = ic;
    }
    private Listbox listbox1 = new Listbox();

    public Listbox getListbox1() {
        return listbox1;
    }

    public void setListbox1(Listbox l) {
        this.listbox1 = l;
    }
    private DropDown dropDown3 = new DropDown();

    public DropDown getDropDown3() {
        return dropDown3;
    }

    public void setDropDown3(DropDown dd) {
        this.dropDown3 = dd;
    }

    public approveW2() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here

        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("approveW2 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }

    // </editor-fold>
    // Perform application initialization that must complete
    // *after* managed components are initialized
    // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
        if (this.isPostBack()) {
        } else {
            String sMeetingid;
            String sTime;
            String eTime;
            Date sDate;
            Date eDate;
            String sCname;
            String sDept;
            String sStatus;
            String sAbstract;
            int iRoomid;
            sMeetingid = this.getSessionBean1().getSUUID();
            //连接数据库
            taskDao.setDataSource(ds);
            String sapp = this.getSessionBean1().getSApp();
            mt = taskDao.getMeeting(sMeetingid, sapp);
            sTime = mt.getSStime();
            eTime = mt.getSEtime();
            sDate = mt.getDSdate();
            eDate = mt.getDEdate();
            sCname = mt.getSCname();
            sDept = mt.getSCinitdept();
            sStatus = mt.getCStatus();
            sAbstract = mt.getSCabstract();
            iRoomid = mt.getIRoomid();
            this.getDropDown1().setSelected(sTime);
            this.getDropDown2().setSelected(eTime);
            this.getCalendar1().setText(sDate);
            this.getCalendar2().setText(eDate);
            this.getDropDown3().setSelected(sAbstract); //警用器械选择
            Object mtId = 0;
            mtId = this.getSessionBean1().getSUUID();
            try {
                CachedRowSet crs = this.getSessionBean1().getV_attendeeRowSet();
                crs.setObject(1, mtId);
                crs.execute();
            } catch (SQLException ex) {
                Logger.getLogger(admin.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
        t_hardwareDataProvider.close();
        v_attendeeDataProvider.close();
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    public String button1_action() {
        // TODO: Replace with your code
        //同意使用工作区
        String sOpinion = "";
        if (this.getTextField3().getText() == null) {
            sOpinion = "同意";
        } else {
            sOpinion = this.getTextField3().getText().toString();
        }
        int iGrp;
        int ibj = 0;
        int iMyGrp;
        String ls_sdate = "";
        String ls_edate = "";
        String ls_stime = "";
        String ls_etime = "";
        String ls_room = "";
        String ls_abstract = "";
        ls_stime = this.getDropDown1().getSelected().toString();
        ls_etime = this.getDropDown2().getSelected().toString();
        this.getSessionBean1().setSSdate(ls_stime);
        this.getSessionBean1().setSEdate(ls_etime);
        int li_usrid = this.getSessionBean1().getIusrid();
        Date ld_sdate = null;
        Date ld_edate = null;
        Date ldt_sdate = null;
        Date ldt_edate = null;
        Date ldt_now = null;
        long ll_s = 0;
        long ll_e = 0;
        long ll_now = 0;
        int li_cnt = 0;
        Iterator lstT = this.getListbox1().getListItems();
        if(lstT.hasNext()){
            li_cnt=1;
        }
        ls_abstract = this.getDropDown3().getSelected().toString(); //警用器械
        ld_sdate = (Date) this.getCalendar1().getValue();
        ld_edate = (Date) this.getCalendar2().getValue();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        ls_sdate = sdf.format(ld_sdate);
        ls_edate = sdf.format(ld_edate);
        ls_sdate = ls_sdate + " " + ls_stime + ":00";
        ls_edate = ls_edate + " " + ls_etime + ":00";
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            ldt_sdate = sdf2.parse(ls_sdate);
            ldt_edate = sdf2.parse(ls_edate);
        } catch (ParseException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        }
        ldt_now = new Date();
        ll_s = ldt_sdate.getTime();
        ll_e = ldt_edate.getTime();
        ll_now = ldt_now.getTime();
        if (ll_s >= ll_e) {
            info("开始时间必须早于截止时间");
            return null;

        }

        /*
        if (ll_s < ll_now) {
        info("开始时间必须晚于当前时间");
        return null;

        }
         */
        if (li_cnt <=0) {
            info("请至少选择一个出警法警");
            return null;

        }
        String sMeetingid = this.getSessionBean1().getSUUID();
        taskDao.setDataSource(ds);
        setupParam stp = new setupParam();
        stp = taskDao.getParam();
        bgs = stp.getBgs();
        jsk = stp.getJsk();
        kmr = stp.getKmr();
        fjd = stp.getFjd();
        jcz = stp.getJcz();

        //取会议室类别
        int li_confirmgrpid = 0;
        //先到先得的情况需要判断下会议室的占用情况
        String sapp = this.getSessionBean1().getSApp();
        int li_roomid = 19;
        int li_roomsort = 7;
        //更新一部分内容，包括时间和地点
        taskDao.updateTimeNroom(sMeetingid, ls_sdate, ls_edate, ls_stime, ls_etime, li_roomid, sapp);
        //取得更新过的
        mt = taskDao.getMeeting(sMeetingid, sapp);
        iGrp = mt.getIConfirmByGrpId();
        iMyGrp = taskDao.getGrpId(li_usrid, 1);

        int iStatus = 0;
        int iUsrid;
        int iroomid;
        iUsrid = this.getSessionBean1().getIusrid();
        iStatus = mt.getIStatus();
        String sTip = null;
        //网上派警
        //法警队确认
        iStatus = 9;
        ibj = 9;
        li_confirmgrpid = jcz;
        sTip = "申请已成功核准！";
        //确认一个工作区,bj=0 为本科室确认 bj=1 为技术科确认 bj=2 为法警队确认 bj=3 为检察长确认
        taskDao.confirmWork(sMeetingid, sOpinion, iStatus, li_confirmgrpid, ibj, iUsrid, ls_abstract);
        //重新取得mt
        mt = taskDao.getMeeting(sMeetingid, sapp);
        //给所有参会人员发消息
        taskDao.sendRTX(mt, ibj,stp);
        //显示提示信息
        this.getSessionBean1().setSTip(sTip);
        //cls
        this.getSessionBean1().setIMtHost(0);
        this.getSessionBean1().setIMtInitdept(0);
        this.getSessionBean1().setIroomid(0);
        this.getSessionBean1().setSEdate("");
        this.getSessionBean1().setSSdate("");
        this.getSessionBean1().setSUUID("");
        this.getSessionBean1().setSMtAbstract("");
        this.getSessionBean1().setSMtName("");
        this.getSessionBean1().setSMtNotice("");
        this.getSessionBean1().setSRefMtId("");

        return "toSucc2";
    }

    public String hyperlink1_action() {
        // TODO: Replace with your code
        String ls_meetingid;
        ls_meetingid = this.getSessionBean1().getSUUID();
        HttpSession hs;
        hs = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        if (hs != null) {
            hs.setAttribute("cmeetingid", ls_meetingid);
        }
        //取会议室类别
        //网上派警
        return "toPrntODC";
    }

    public String button2_action() {
        // TODO: Replace with your code
        //否决
        String sOpinion = "";
        if (this.getTextField3().getText() == null) {
            sOpinion = "不同意";
        } else {
            sOpinion = this.getTextField3().getText().toString();
        }


        int ibj = 0;

        String sMeetingid = this.getSessionBean1().getSUUID();
        taskDao.setDataSource(ds);
        String sapp = this.getSessionBean1().getSApp();
        mt = taskDao.getMeeting(sMeetingid, sapp);
        int iStatus;
        int iUsrid;
        iUsrid = this.getSessionBean1().getIusrid();
        iStatus = 4;
        String sTip = "申请已成功否决，相关提示信息已发往申请人！";
        //最后确认
        taskDao.vetoMeeting(sMeetingid, sOpinion, iStatus, iUsrid, ibj, sapp);
        //重新取得mt
        mt = taskDao.getMeeting(sMeetingid, sapp);
        //给申请人发信息
        //给办公室发信息
        ibj = 4;
        setupParam stp = new setupParam();
        stp = taskDao.getParam();
        taskDao.sendRTX(mt, ibj,stp);
        //显示提示信息
        this.getSessionBean1().setSTip(sTip);
        //cls
        this.getSessionBean1().setIMtHost(0);
        this.getSessionBean1().setIMtInitdept(0);
        this.getSessionBean1().setIroomid(0);
        this.getSessionBean1().setSEdate("");
        this.getSessionBean1().setSSdate("");
        this.getSessionBean1().setSUUID("");
        this.getSessionBean1().setSMtAbstract("");
        this.getSessionBean1().setSMtName("");
        this.getSessionBean1().setSMtNotice("");

        return "toSucc2";
    }

    public String button3_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        //去到人员选择界面
        return "toChoice2";
    }


}

